package demo.example.demo.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import demo.example.demo.DTO.FaceContrastVerifyRequest;
import demo.example.demo.DTO.LivingDetectInitRequest;
import demo.example.demo.DTO.LivingdetectVerifyRequest;
import demo.example.demo.base.gateway.AlgorithmE;
import demo.example.demo.base.gateway.ESGateway;
import demo.example.demo.base.gateway.entity.GatewayResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.net.URLDecoder;

@Slf4j
@Controller
public class LivingDetectionController {


    @RequestMapping("livingDetection/index")
    public String indexTo() {
        return "fldt/sdk_living_detection/living_index";
    }

    @RequestMapping("livingDetection/result")
    public String toGetResult(@RequestParam String token , @RequestParam String verifyMsg, @RequestParam String code, @RequestParam String msg, Model model) {
        model.addAttribute("token",token);
        model.addAttribute("verifyMsg",verifyMsg);
        model.addAttribute("code",code);
        model.addAttribute("msg",msg);
        return "fldt/sdk_living_detection/living_getResult";
    }

    @RequestMapping("livingDetection/popupIndex")
    public String popupIndexTo() {
        return "fldt/popup_living_detection/living_index";
    }

    @PostMapping("livingDetection/init")
    @ResponseBody
    public String init(String initMsg) throws Exception {
        log.info("请求参数:{}", initMsg);
        String appcode = "你的appcode";
        String key = "你的密钥";
        AlgorithmE algo = AlgorithmE.MD5;

        LivingDetectInitRequest livingDetectInitRequest = new LivingDetectInitRequest();
        livingDetectInitRequest.setInitMsg(initMsg);

        ESGateway esGateway = new ESGateway(appcode, algo, key);
        GatewayResponse gatewayResponse = esGateway.sendToGateWay(livingDetectInitRequest.act, livingDetectInitRequest.toJsonStr());
        log.info("服务器端返回：{}",gatewayResponse.toJsonStr());
        if(gatewayResponse == null || gatewayResponse.getCode().equals("-1") || StringUtil.isBlank(gatewayResponse.getBizContent())){
            return "服务异常" + gatewayResponse.getCode() + gatewayResponse.getMsg();
        }
        return gatewayResponse.getBizContent();
    }

    @PostMapping("livingDetection/verify")
    @ResponseBody
    public String verify(String token, String verifyMsg, Boolean captureFace) {
        log.info("请求参数:{}=={}", token, verifyMsg, captureFace);
        String appcode = "你的appcode";
        String key = "你的密钥";
        AlgorithmE algo = AlgorithmE.MD5;

        LivingdetectVerifyRequest livingdetectVerifyRequest = new LivingdetectVerifyRequest();
        livingdetectVerifyRequest.setToken(token);
        livingdetectVerifyRequest.setVerifyMsg(verifyMsg);

        ESGateway esGateway = new ESGateway(appcode, algo, key);
        GatewayResponse gatewayResponse = esGateway.sendToGateWay(livingdetectVerifyRequest.act, livingdetectVerifyRequest.toJsonStr());
        log.info("服务器端返回：{}",gatewayResponse.toJsonStr());
        if(gatewayResponse == null || gatewayResponse.getCode().equals("-1") || StringUtil.isBlank(gatewayResponse.getBizContent())){
            return "服务异常" + gatewayResponse.getCode() + gatewayResponse.getMsg();
        }
        return gatewayResponse.getBizContent();
    }
}
